package com.manage.platform.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.platform.dto.OrganizeMemberDto;
import com.manage.platform.entity.OrganizeMemberEntity;
import com.manage.platform.mapper.OrganizeMemberMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * YWD
 * 2025/9/26
 */
@AllArgsConstructor
@Service
public class OrganizeMemberServiceImpl extends ServiceImpl<OrganizeMemberMapper, OrganizeMemberEntity> {

    public Boolean removeByOrgId(Long orgId) {
        LambdaUpdateWrapper<OrganizeMemberEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OrganizeMemberEntity::getOrgId, orgId);
        return remove(updateWrapper);
    }


    public Boolean saveMember(Long memberId, List<Long> departmentId) {
        List<OrganizeMemberEntity> list = departmentId.stream()
                .map(departmentId1 -> {
                    OrganizeMemberEntity organizeMemberEntity = new OrganizeMemberEntity();
                    organizeMemberEntity.setMemberId(memberId);
                    organizeMemberEntity.setOrgId(departmentId1);
                    return organizeMemberEntity;
                }).toList();
        return saveBatch(list);
    }


    public Boolean removeByMemberId(Long memberId) {
        LambdaUpdateWrapper<OrganizeMemberEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(OrganizeMemberEntity::getMemberId, memberId);
        return remove(updateWrapper);
    }

    public Boolean saveByOrgIdAndMemberId(OrganizeMemberDto dto) {
        List<Long> memberIdList = dto.getOrganizeMemberIdList();
        if (CollectionUtil.isNotEmpty(memberIdList)) {
            List<OrganizeMemberEntity> list = new ArrayList<>();
            memberIdList.forEach(memberId -> {
                //是否存在
                LambdaQueryWrapper<OrganizeMemberEntity> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(OrganizeMemberEntity::getOrgId, dto.getOrgId());
                queryWrapper.eq(OrganizeMemberEntity::getMemberId, memberId);
                boolean exists = this.baseMapper.exists(queryWrapper);
                if (!exists) {
                    OrganizeMemberEntity organizeMemberEntity = new OrganizeMemberEntity();
                    //组织ID
                    organizeMemberEntity.setOrgId(dto.getOrgId());
                    //成员ID
                    organizeMemberEntity.setMemberId(memberId);
                    list.add(organizeMemberEntity);
                }
            });
            if(CollectionUtil.isNotEmpty(list)){
                this.saveBatch(list);
            }
        }
        return true;
    }

    public Boolean removeByOrgIdAndMemberId(OrganizeMemberDto dto) {
        LambdaUpdateWrapper<OrganizeMemberEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OrganizeMemberEntity::getOrgId, dto.getOrgId())
                .in(OrganizeMemberEntity::getMemberId, dto.getOrganizeMemberIdList());
        return remove(updateWrapper);
    }


}
